Skip to content

Protect Against Potential Memory L eak and IWYU Cleanups#813

Merged
Youw merged 6 commits into
libusb:masterfrom
JHartzer:static-analysis
Jun 14, 2026
Merged

Protect Against Potential Memory L eak and IWYU Cleanups#813
Youw merged 6 commits into
libusb:masterfrom
JHartzer:static-analysis

Conversation

@JHartzer

@JHartzer JHartzer commented May 21, 2026

Copy link
Copy Markdown
Contributor

Description

This PR resolves a potential memory leak identified via static analysis in the Linux backend. It also implements several IWYU header fixes.

Problems Identified

  1. Linux Memory Leak: There is a potential memory leak in linux/hid.c parse_uevent_info where multiple matching HID_UNIQ or HID_NAME keys in a uevent file could overwrite previously allocated buffers without freeing them first.
  2. Transitive Include Fragility: Several source files (libusb/hid.c and mac/hid.c) relied on transitive inclusions for standard types like uint8_t, standard functions like calloc, and error definitions like errno. If parent headers are refactored, these sources will break.
  3. Unused Headers: Unused headers (such as <sys/utsname.h> and <linux/version.h>) were included in multiple places, unnecessarily increasing compile times.

Proposed Changes

  • Linux Memory Leak Fix
    • Updated the Linux backend linux/hid.c to safely free any previously allocated memory before saving a new device property. This stops memory from leaking if a device's info key (like name or serial) is parsed more than once.
  • IWYU (Include-What-You-Use) Header Cleanups
    • Added missing headers (like <stdint.h>, <stdlib.h>, and <errno.h>) across the test utility, macOS, and LibUSB backends so they don't crash or break when parent files are updated.
    • Removed unused headers (like <sys/utsname.h> and <linux/version.h> ) that were cluttering the code and slowing down compile times.

Verification & Testing

1. Automated Compilation

Clean out-of-source builds were configured and executed using CMake:

cmake -B build -S . -DHIDAPI_WITH_LIBUSB=OFF -DHIDAPI_BUILD_HIDTEST=ON
cmake --build build
  • Result: All targets compiled with zero warnings and zero errors.

2. Static Analysis

A verification pass using clang-tidy was conducted on the Linux backend:

clang-tidy -p build-tidy linux/hid.c

  • Result: The Potential memory leak warning has been successfully resolved.

3. Functional Verification

The compiled test utility hidtest_hidraw was run successfully. It executed the hid_enumerate() discovery loop using our modified parse_uevent_info implementation, successfully extracting and displaying all connected USB device attributes

@mcuee mcuee added hidraw Related to Linux/hidraw backend libusb Related to libusb backend labels May 21, 2026
Comment thread hidtest/test.c

@Youw Youw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment, everything else looks good, thanks

@JHartzer

Copy link
Copy Markdown
Contributor Author

Hey @Youw! I just pushed up a suggested fix for the windows-cmake build failures. It looks like windows-latest runner was updated to a newer Visual Studio version (in this case, Visual Studio 2026 / VS 18), so the hard-coded VS path didn't exist. This seems to be a standard, flexible way to find the Visual Studio installation path.

@Youw

Youw commented Jun 14, 2026

Copy link
Copy Markdown
Member

Thanks.

I'd like to keep these things separated. Let this PR be only about the code and not about the CI. Temporary failed MSVC build will not stop me from merging it (CI issue is not caused by the PR, so it is fine).

Regarding MSVC build fix/improvement - I agree, your suggested fix is so much better than the hard-coded version that is now, but I'd rather make even one more step further, and use setup-msvc-dev Github Action.
But thatshould be a separate PR a separte PR.

Resolve the builds.yml conflict in favour of master: the hard-coded vcvars
path is now fixed on master by the setup-msvc-dev action plus a dedicated
windows-mingw job (libusb#817), so this branch's vswhere-based CI change is dropped
as redundant. The static-analysis code changes (the linux/hid.c memory-leak
fix and the IWYU header cleanups) are unaffected.

Assisted-by: claude-code:claude-opus-4-8
@Youw Youw merged commit ee022f5 into libusb:master Jun 14, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hidraw Related to Linux/hidraw backend libusb Related to libusb backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants